<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/17
 * Time: 9:55
 */
namespace Home\Controller;
use Think\Controller;
class InstallController extends CommonController{
    // 登录系统后的展示页，展示分日信息
        public function index(){
            $date_begin = date('Y-m-d',strtotime('-7 days'));
            $date_end = date('Y-m-d',strtotime('-1 days'));
            $this -> assign('date_begin',$date_begin);
            $this -> assign('date_end',$date_end);
            $this -> display();
        }
        // 按渠道展示 7日数据入口页面
        public function channeldetail(){
            $channel_id = I('get.channel_id');
            $b_date = I('get.bdate');
            $e_date = I('get.edate');
            if($b_date && $e_date){
                $date_begin = $b_date;
                $date_end = $e_date;
            }else{
                $date_begin = date('Y-m-d',strtotime('-7 days'));
                $date_end = date('Y-m-d',strtotime('-1 days'));
            }

            $this -> assign('date_begin',$date_begin);
            $this -> assign('date_end',$date_end);
            $this -> assign('channel_id',$channel_id);
            $this -> display();
        }
        // 获取最近7日内的统计信息 包括：安装、卸载、日活、当日安装后卸载
        public function getSevenStastic(){
            $aData = array();
            $b_date = I('post.begin_date');
            $e_date = I('post.end_date');
            if(!$b_date && !$e_date){
                $e_date = date("Y-m-d",strtotime('-1 days'));
                $b_date = date("Y-m-d",strtotime('-7 days'));
            }else{
                $e_date = date("Y-m-d",strtotime($e_date));
                $b_date = date("Y-m-d",strtotime($b_date));
            }
            // 使用流水表获取表名
            /* 2016-06-23 修改为使用统计表中的数据
            else{
                $days = $this ->trend_days();
            }
            if($days){
                foreach($days as $day){
                    $table_name = "epduba_lieying_install_".$day;
                    $data = D('Install')->getCountsByField($table_name,'active');
                    $InstallUn = count(D('Install') -> getInstallUnCount($table_name));
                    $data['4'] = $InstallUn;
                    $aData[$day] = $data;
                }
            }*/
            $data = D('Statistic') -> getCountsDur($b_date,$e_date);
            foreach($data as $val){
                $key = date('Ymd',strtotime($val['statistic_date']));
                $aData[$key] = array(
                    'install' => $val['install'],
                    'uninstall' => $val['uninstall'],
                    'active' => $val['active'],
                    'installun' => $val['installun'],
                );
            }
            $this->ajaxReturn($aData);
        }
        // 按渠道获取最近七日的数据详细数据
        public function getChannelStatistic(){
            $cData = array();
            $b_date = I('post.begin_date');
            $e_date = I('post.end_date');
            if($b_date && $e_date){
                $days = $this ->trend_days($b_date,$e_date);
            }else{
                $days = $this ->trend_days();
            }
            if($days){
                foreach($days as $day){
                    $table_name = "epduba_lieying_install_".$day;
                    $data = D('Install') -> getCountsByChannel($table_name);
                    $channel_array = array();
                    foreach($data as $d){
                        $channel_id = ($d['channel_id'])?:'0000';
                        switch($d['active']){
                            case '1':
                                $channel_array[$channel_id]['install'] = $d['c_count'];
                                break;
                            case '2':
                                $channel_array[$channel_id]['uninstall'] = $d['c_count'];
                                break;
                            case '3':
                                $channel_array[$channel_id]['active'] = $d['c_count'];
                                break;
                        }
                    }
                    $cData[$day] = $channel_array;
                }
                return $this -> ajaxReturn($cData);
            }

        }

        // 按渠道获取最近七日 各个数据的总和
        public function getChannelSum(){
            $cData = array();
            $b_date = I('post.begin_date');
            $e_date = I('post.end_date');
//            if($b_date && $e_date){
//                $days = $this ->trend_days($b_date,$e_date);
//            }else{
//                $days = $this ->trend_days();
//            }
//            if($days){
//                foreach($days as $day){
//                    $table_name = "epduba_lieying_install_".$day;
//                    $data = D('Install')->getCountsByChannel($table_name);
//                    $tData = $sData = array();
//                    foreach($data as $key => $val){
//                        $channel_id = ($val['channel_id'])?:"0000";
//                        switch($val['active']){
//                            case 1:
//                                $sData['install'] = $val['c_count'];
//                                $cData[$channel_id]['install'] += $val['c_count'];
//                                break;
//                            case 2:
//                                $sData['uninstall'] = $val['c_count'];
//                                $cData[$channel_id]['uninstall'] += $val['c_count'];
//                                break;
//                            case 3:
//                                $sData['active'] = $val['c_count'];
//                                $cData[$channel_id]['active'] += $val['c_count'];
//                                break;
//                        }
//                    }
//                    $InstallUn_array = D('Install') -> getInstallUnCountByChannel($table_name);
//                    if($InstallUn_array){
//                        foreach($InstallUn_array as $key => $instalun_count){
//                            $sData['installun'] = $instalun_count;
//                            $cData[$key]['installun'] += $instalun_count;
//                        }
//                    }
//                }
//                $this -> ajaxReturn($cData);
//            }
            if(!$b_date && !$e_date){
                $e_date = date("Y-m-d",strtotime('-1 days'));
                $b_date = date("Y-m-d",strtotime('-7 days'));
            }else{
                $e_date = date("Y-m-d",strtotime($e_date));
                $b_date = date("Y-m-d",strtotime($b_date));
            }
            $result = D('Statistic') -> getChannelCountByDate($b_date,$e_date);
            foreach($result as $item) {
                $channel = $item['channel_id'];
                unset($item['channel_id']);
                $cData[$channel] = $item;
            }
            $this -> ajaxReturn($cData);
        }
        //获取固定渠道号下7日数据列表
        public function getDataByChannel(){
            $cData = array();
            $channel_id = I('post.channel_id');
            if($channel_id != null){
                $b_date = I('post.begin_date');
                $e_date = I('post.end_date');

                if(!$b_date && !$e_date){
                    $e_date = date("Y-m-d",strtotime('-1 days'));
                    $b_date = date("Y-m-d",strtotime('-7 days'));
                }else{
                    $e_date = date("Y-m-d",strtotime($e_date));
                    $b_date = date("Y-m-d",strtotime($b_date));
                }
                //通过日志流水表中统计
                /*
                if($b_date && $e_date){
                    $days = $this ->trend_days($b_date,$e_date);
                }else{
                    $days = $this ->trend_days();
                }
                if($days){
                    foreach($days as $day){
                        $table_name = "epduba_lieying_install_".$day;
                        $eqdata = array();
                        $eqstatistic = D('Install') -> getStatByChannel($table_name,$channel_id);
                        if($eqstatistic){
                            foreach($eqstatistic as $eqd){
                                switch($eqd['active']){
                                    case '1':
                                        $eqdata['install'] = $eqd['u_count'];
                                        break;
                                    case '2':
                                        $eqdata['uninstall'] = $eqd['u_count'];
                                        break;
                                    case '3':
                                        $eqdata['active'] = $eqd['u_count'];
                                        break;
                                }
                            }
                        }
                        //获取该日期 当日安装 后卸载的设备数量
                        $install_un_count = D('Install') -> getInstallUnByChannelDay($table_name,$channel_id);
                        $eqdata['installun'] = $install_un_count;
                        $cData[$day] = $eqdata;
                    }
                }*/
                //现修改为通过统计表 info_epduba_saas_static.epduba_lieying_install_channel 获取
                $data = D('Statistic') -> getDayByChannel($channel_id,$b_date,$e_date);
                if($data){
                    foreach($data as $dval){
                        $dkey = date("Ymd",strtotime($dval['statistic_date']));
                        $cData[$dkey]['install'] = $dval['install'];
                        $cData[$dkey]['uninstall'] = $dval['uninstall'];
                        $cData[$dkey]['active'] = $dval['active'];
                        $cData[$dkey]['installun'] = $dval['installun'];
                    }
                }
            }
            $this -> ajaxReturn($cData);
        }
        //总量统计接口
        public function getAllData(){
            $data = array('install' => 0,'uninstall' => 0,'active' => 0,'installun' => 0);
            $new_data = array();
            $date = date("Ymd",strtotime('-1 days'));
            $table_name = 'epduba_lieying_install_'.$date;
            $result = D('Install') -> getDateData($table_name);
            foreach($result as $today_data){
                switch($today_data['active']){
                    case '1':
                        $today_data_tmp['install'] = $today_data['u_count'];
                        break;
                    case '2':
                        $today_data_tmp['uninstall'] = $today_data['u_count'];
                        break;
                    case '3':
                        $today_data_tmp['active'] = $today_data['u_count'];
                        break;
                }
            }
            //今日安装后又卸载的数据
            $ret = D('Install') -> getInstallUnAllData($table_name);
            $today_data_tmp['installun'] = $ret;
            //读取历史数据
            $context = file_get_contents('StatisticDateData.json');
            if($context){
                $data = json_decode($context,TRUE);
                $new_install = intval($data['install']) + intval($today_data_tmp['install']);
                $new_uninstall =intval($data['uninstall']) + intval($today_data_tmp['uninstall']);
                $new_active =intval($data['active']) + intval($today_data_tmp['active']);
                $new_installun =intval($data['installun']) + intval($today_data_tmp['installun']);
                $new_data = array(
                    'install' => $new_install,
                    'uninstall' => $new_uninstall,
                    'active' => $new_active,
                    'installun' => $new_installun
                );
            }
            $this -> ajaxReturn($new_data);
        }
        /**
         * 获取开始日期 结束日期之间的日期数组
         * @param null $start_day 开始日期
         * @param null $end_day 结束日期
         * @return array 日期数组
         */
        private function trend_days($start_day = null,$end_day = null){
            $select_days = array();
            // 默认获取 7日内时间
            if($start_day == null){
                $end_day = date("Ymd",strtotime('-1 days'));
                for($i = 6;$i>0;$i--){
                    $select_days[] = date('Ymd',strtotime('-'.($i+1).' days'));
                }
                $select_days[] = $end_day;
            }
            // 选择开始日期，没有结束日期，获取到昨日,有结束日期获取中间的时间
            if($start_day != null){
                if($end_day == null){
                    $endtime = strtotime('-1 days');
                }else{
                    $endtime = strtotime($end_day);
                }
                $starttime = strtotime($start_day);
                if($starttime && $endtime){
                    while($starttime <= $endtime){
                        $select_days[] = date('Ymd',$starttime);
                        $starttime += 60 * 60 * 24;
                    }
                }
            }
            return $select_days;
        }
    }